package cn.zlj.nmcc.web.controller;

import cn.zlj.nmcc.domain.Blacklist;
import cn.zlj.nmcc.fegin.UserService;
import cn.zlj.nmcc.service.IMessageSmsService;
import cn.zlj.nmcc.domain.MessageSms;
import cn.zlj.nmcc.query.MessageSmsQuery;
import cn.zlj.nmcc.result.JSONResult;
import cn.zlj.nmcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/messageSms")
public class MessageSmsController {

    @Autowired
    public IMessageSmsService messageSmsService;
    @Autowired
    private UserService userService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody MessageSms messageSms) {
        if (messageSms.getId() != null) {
            messageSmsService.updateById(messageSms);
        } else {
            messageSmsService.insert(messageSms);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        messageSmsService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(messageSmsService.selectById(id));
    }

    //getByUser
    @RequestMapping(value = "/getByUser/{id}", method = RequestMethod.GET)
    public JSONResult getByUser(@PathVariable("id") Long id) {
        return JSONResult.success(userService.get(id));
    }
    //添加黑名单
    @RequestMapping(value = "/blacklist", method = RequestMethod.POST)
    public JSONResult saveOrUpdateBlacklist(@RequestBody Blacklist blacklist) {
        return JSONResult.success(userService.saveOrUpdate(blacklist));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(messageSmsService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody MessageSmsQuery query) {
        Page<MessageSms> page = new Page<MessageSms>(query.getPage(), query.getRows());
        page = messageSmsService.selectPage(page);
        return JSONResult.success(new PageList<MessageSms>(page.getTotal(), page.getRecords()));
    }
}
